$(function () {
    let layer = layui.layer
    $('#logout').on('click', function () {
        layer.confirm('是否要退出登录?', { icon: 3, title: '提示' }, function (index) {

            layer.close(index);
            location.href = '/login.html'
            localStorage.removeItem('token')
            layer.close(index)
        });

    })
    getUserInfo()
})
// 不能写在入口函数里面 要是全局样式
function getUserInfo() {
    axios({
        method: 'GET',
        url: '/my/userinfo',
        // headers:{
        //     Authorization:localStorage.getItem('token')
        // }

    }).then((res) => {
        // console.log(res.data)
        if (res.data.status !== 0) {
            return layui.layer.msg(res.data.message)
        }
        renderUserInfo(res.data.data)
    })
}

function renderUserInfo(user) {
    // console.log(user)
    let name = user.nickname || user.username
    $('.welcome').html('欢迎 &nbsp &nbsp' + name)

    if (user.user_pic == null) {
        $('.avatar-text').show().html(name[0].toUpperCase())
        $('.avatar-img').hide()
    } else {
        $('.avatar-text').hide()
        $('.avatar-img').show().attr('src', user.user_pic)
    }

}